export const cookie = {
  set(name, value, config = {}) {
    var cfg = {
      expires: null,
      path: null,
      domain: null,
      secure: false,
      httpOnly: false,
      ...config
    }
    var cookieStr = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`
    if (cfg.expires) {
      var exp = new Date()
      exp.setTime(exp.getTime() + parseInt(cfg.expires) * 1000)
      cookieStr += `;expires=${exp.toGMTString()}`
    }
    if (cfg.path) {
      cookieStr += `;path=${cfg.path}`
    }
    if (cfg.domain) {
      cookieStr += `;domain=${cfg.domain}`
    }
    if (cfg.secure) {
      cookieStr += `;secure`
    }
    if (cfg.httpOnly) {
      cookieStr += `;httpOnly`
    }
    document.cookie = cookieStr
  },
  get(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + encodeURIComponent(name) + "=([^;]*)(;|$)"))
    if (arr != null) {
      return decodeURIComponent(arr[2])
    } else {
      return null
    }
  },
  remove(name) {
    var exp = new Date()
    exp.setTime(exp.getTime() - 1)
    document.cookie = `${encodeURIComponent(name)}=;expires=${exp.toGMTString()}`
  }
}